package com.pandora.common.util;

import java.io.BufferedInputStream;
import java.io.InputStream;
import com.pandora.framework.constant.Constants;
import com.pandora.framework.util.LogUtils;

/**
 * 文件IO操作工具类
 * 
 * @author JingChenglong 2017/12/26 21:40
 *
 */
public class FileUtils {

	/** 缓冲区大小 */
	private static final int BUFFERSIZE = 8196;

	/**
	 * 将输入流生成内容
	 * 
	 * @param inputStream
	 * @return
	 */
	public static String generateContent(InputStream inputStream) {

		if (inputStream == null) {
			return null;
		}

		BufferedInputStream bufferedInputStream = null;

		StringBuffer content = new StringBuffer();

		try {

			byte[] buffer = new byte[BUFFERSIZE];
			int count = 0;

			bufferedInputStream = new BufferedInputStream(inputStream, BUFFERSIZE);

			while ((count = bufferedInputStream.read(buffer)) != -1) {
				content.append(new String(buffer, 0, count, Constants.ENCODEING_DEFAULT));
			}

			buffer = null;

			return content.toString();
		} catch (Exception e) {
			LogUtils.logError(CommonUtils.getTraceInfo() + e.getMessage());
			return null;
		} finally {
			StreamUtils.closeBufferedInputStream(bufferedInputStream);
			StreamUtils.closeInputStream(inputStream);
		}
	}
}